package com.dailyblue.java.mybatis.mybatis02.test;

import com.dailyblue.java.mybatis.mybatis02.bean.Emp;
import com.dailyblue.java.mybatis.mybatis02.bean.JsonResult;
import com.dailyblue.java.mybatis.mybatis02.mapper.EmpMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class App {
    private static SqlSession session;
    private static EmpMapper mapper;
    private static Scanner scan;

    static {
        try {
            scan = new Scanner(System.in);
            InputStream in = Resources.getResourceAsStream("config.xml");
            SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
            session = ssf.openSession();
            mapper = session.getMapper(EmpMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void a() throws IOException {
        Emp emp = mapper.findEmpByNo(6);
        System.out.println(emp);
        session.close();
    }

    private static void a1() throws IOException {
        Emp emp = mapper.findEmpByNo1(6);
        System.out.println(emp);
        session.close();
    }

    private static void a2() throws IOException {
        Emp emp = mapper.findEmpByNo2(6);
        System.out.println(emp);
        session.close();
    }

    private static void b() throws IOException {
        Emp emp = mapper.findEmpByEname("方文山");
        System.out.println(emp);
        session.close();
    }

    // 错误
    private static void c() throws IOException {
        List<Emp> list = mapper.findEmpByJobAndLessSal("MANAGER", 3000.0);
        list.forEach(System.out::println);
        session.close();
    }

    private static void c1() throws IOException {
        Emp emp = new Emp();
        emp.setJob("MANAGER");
        emp.setSal(3000.0);
        List<Emp> list = mapper.findEmpByJobAndLessSal1(emp);
        list.forEach(System.out::println);
        session.close();
    }

    private static void c2() throws IOException {
        Map map = new HashMap();
        map.put("job", "MANAGER");
        map.put("sal", 3000);
        List<Emp> list = mapper.findEmpByJobAndLessSal2(map);
        list.forEach(System.out::println);
        session.close();
    }

    private static void c3() throws IOException {
        List<Emp> list = mapper.findEmpByJobAndLessSal3("MANAGER", 3000.0);
        list.forEach(System.out::println);
        session.close();
    }

    private static void d1() {
        System.out.print("请输入页数：");
        int page = scan.nextInt();
        int size = 5;
        int total = mapper.findAllEmpCount();
        List<Emp> list = mapper.findEmpByPage((page - 1) * size, size);
        int nums = total % size == 0 ? (total / size) : (total / size + 1);
        size = list.size();
        JsonResult<Emp> result = new JsonResult<>(page, size, total, nums, list);
        System.out.println(result);
    }

    private static void d2(){
        System.out.print("请输入页数：");
        int page = scan.nextInt();
        PageHelper.startPage(page,5);
        List<Emp> list = mapper.findAll();
        PageInfo<Emp> pageInfo = new PageInfo<>(list);
        System.out.println(pageInfo);
    }

    public static void main(String[] args) throws IOException {
        a1();
    }
}
